$(function () {
    $("#jqGrid").Grid({
        url: '../scrapaccerecord/list',
        colModel: [

            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '报废编号', name: 'scrapNo', index: 'SCRAP_NO', width: 80},
            {label: '创建日期', name: 'createDate', index: 'CREATE_DATE', width: 80,formatter:function (value) {
                    return transDate(value,'yyyy-MM-dd')
                }},
            {label: '审批人', name: 'checkUser', index: 'CHECK_USER', width: 80},
            {
                label: '审批时间', name: 'checkDate', index: 'CHECK_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '审核状态', name: 'status', index: 'STATUS', width: 80, formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value]
                }
            },
            {
                label: '是否已处理', name: 'isDeal', index: 'IS_DEAL', width: 80, formatter: function (value) {
                    return sysEnums.YES_OR_NO[value]
                }
            },
            {label: '处理人', name: 'dealUser', index: 'DEAL_USER', width: 80},
            {
                label: '处理时间', name: 'dealTime', index: 'DEAL_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '操作', name: 'id', index: 'ID',width: 80, align: 'center', sortable: false, formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.getScrapDetail(\""+row.scrapNo+"\")'><i class='fa fa-location-arrow'></i>&nbsp;报废记录</button>";
                }
            },
            /* {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},
            {label: '更新日期', name: 'updateDate', index: 'UPDATE_DATE', width: 80},
            {label: '更新者ID', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            {label: '创建者ID', name: 'createUser', index: 'CREATE_USER', width: 80}  */]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        scrapAcceRecord: {
            status: '',
            id: '',
            checkDate: '',
            checkUser: '',
            remark: '',
            dealTime: '',
            dealUser: '',
            isDeal: '',
            scrapNo: '',
            delFlag: '',
            createDate: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
        },

        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            let date = new Date();
            vm.scrapAcceRecord = {
                status: '0',
                id: '',
                checkDate: '',
                checkUser: '',
                remark: '',
                dealTime: '',
                dealUser: '',
                isDeal: '0',
                scrapNo: '',
                delFlag: '',
                createDate: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
            };
            vm.scrapAcceRecord.scrapNo = 'zf-' + date.getFullYear()+ (date.getMonth() + 1) +(date.getDate())+ date.getHours()+date.getMinutes()+date.getSeconds()
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.scrapAcceRecord.id == (null || '') ? "../scrapaccerecord/save" : "../scrapaccerecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.scrapAcceRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../scrapaccerecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../scrapaccerecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.scrapAcceRecord = r.scrapAcceRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getScrapDetail:function (scrapNo) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-location-arrow"></i> 报废记录详细',
                content: '../sys/sparescrapdetail.html?scrapNo=' + scrapNo
            })
        }
    }
});